package servlet;

import impl.UserDaoImpl;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.User;
import dao.UserDao;

/**
 * Servlet implementation class SignUpServlet
 */
public class SignUpServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SignUpServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String info="数据填写不合法!</br>";
		UserDao userDao = new UserDaoImpl();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String uname=request.getParameter("uname");
		String birthS=request.getParameter("birth");
		String salS=request.getParameter("sal");
		String sexS=request.getParameter("ssex");
		if(uname!=null&&birthS!=null&&salS!=null&&sexS!=null){
			try {
				Date birth = (Date) sdf.parse(birthS);
				double sal = Double.valueOf(salS);
				boolean sex;
				if("male".equals(sexS))sex=true;
				else sex=false;
				User u = new User(uname, sal, birth, sex);
				userDao.addUserWithDS(u);
				info="输入成功!</br>";
			} catch(NumberFormatException e1){
				info="工资需要为数字!</br>";
			} catch (ParseException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			
		}
//		System.out.println("uname..."+uname+"birth..."+birthS+"sal..."+salS+"sex..."+sexS);
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter writer=response.getWriter();
		writer.write(info);
		writer.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	}

}
